package com.liming.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.DocumentException;
import org.json.JSONException;

import com.liming.util.CheckUtil;
import com.liming.util.MessageUtil;

public class WeChatServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String signature = req.getParameter("signature");// 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
		String timestamp = req.getParameter("timestamp");// 时间戳
		String nonce = req.getParameter("nonce"); // 随机数
		String echostr = req.getParameter("echostr");// 随机字符串

		PrintWriter out = resp.getWriter();
		if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
			out.print(echostr);
		}
	}

	// 消息接收与响应
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");

		PrintWriter out = resp.getWriter();

		try {
			// 从req中接收的xml转为map
			Map<String, String> map = MessageUtil.xmlToMap(req);
			String fromUserName = map.get("FromUserName");
			String toUserName = map.get("ToUserName");
			String createTime = map.get("CreateTime");
			String msgType = map.get("MsgType");
			String content = map.get("Content");
			String msgId = map.get("MsgId");

			// 要回复的消息内容
			String message = null;
			// 判断是否文本消息
			if (MessageUtil.MESSAGE_TEXT.equals(msgType)) {
				// 判断菜单操作
				if ("1".equals(content)) {
					message = MessageUtil.initText(fromUserName, toUserName, MessageUtil.sendWangYi());
				} else if ("2".equals(content)) {
					// 开心一刻
					message = MessageUtil.initText(fromUserName, toUserName, MessageUtil.sendJOKE());
				} else if ("3".equals(content)) {
					// 图文消息/meimv
					message = MessageUtil.initNewsMessage(fromUserName, toUserName);
				} else if ("4".equals(content)) {
					// 多图文消息/meimv
					message = MessageUtil.initMoreNewsMessage(fromUserName, toUserName);
				} else if ("?".equals(content) || "？".equals(content)) {
					message = MessageUtil.initText(fromUserName, toUserName, MessageUtil.menuText());
				} else {
					message = MessageUtil.initText(fromUserName, toUserName, MessageUtil.menuText());
				}

				// 判断是否事件推送
			} else if (MessageUtil.MESSAGE_EVENT.equals(msgType)) {
				String eventType = map.get("Event");
				// 判断是否订阅
				if (MessageUtil.MESSAGE_EVENT_SUBSCRIBE.equals(eventType)) {
					message = MessageUtil.initText(fromUserName, toUserName, MessageUtil.subscribeMsg());
					// 点击弹出菜单
				} else if (MessageUtil.MESSAGE_CLICK.equals(eventType)) {
					message = MessageUtil.initText(fromUserName, toUserName, MessageUtil.menuText());
					// 点击链接菜单
				} else if (MessageUtil.MESSAGE_VIEW.equals(eventType)) {
					String url = map.get("EventKey");
					System.out.println("点击链接菜单："+url);
					message = MessageUtil.initText(fromUserName, toUserName, url);
					// 扫码事件
				} else if (MessageUtil.MESSAGE_SCANCODE.equals(eventType)) {
					String key = map.get("EventKey");
					System.out.println("扫码："+key);
					message = MessageUtil.initText(fromUserName, toUserName, key);
				}
				// 判断为地理位置
			} else if (MessageUtil.MESSAGE_LOCATION.equals(msgType)) {
				String label = map.get("Label");
				String Location_X = map.get("Location_X");
				String Location_Y = map.get("Location_Y");
				message = MessageUtil.initText(fromUserName, toUserName, label+"("+Location_X+","+Location_Y+")");
 			}

			// 回复消息
			out.println(message);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			out.close();
		}
	}
}
